package cliente.vista;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;

import cliente.controle.ContaControlador;

public class DepositaView {
	private JFrame tela;
	private JPanel painel;
	private JLabel valorLabel;
	private JLabel contaLabel;
	private JTextField valor;
	private JTextField conta;
	private JButton deposita;
	private JButton cancela;
	private ActionListener ouvinte;

	public void iniciaDepositaView() {
		ouvinte = new acao();
		tela = new JFrame();
		tela.setResizable(false);
		tela.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		tela.setBounds(500, 300, 300, 150);
		tela.setTitle("Depósito");
		painel = new JPanel();

		GroupLayout layout = new GroupLayout(painel);
		painel.setLayout(layout);

		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		valorLabel = new JLabel("Valor a Ser Depositado:");
		contaLabel = new JLabel("Conta Aonde Depositar:");
		valor = new JTextField();
		conta = new JTextField();
		deposita = new JButton("Depositar");
		deposita.addActionListener(ouvinte);
		cancela = new JButton("Cancelar");
		cancela.addActionListener(ouvinte);

		organizaFields(layout);

		tela.add(painel);
		tela.setVisible(true);
	}

	private void criaGruposVerticais(GroupLayout layout) {
		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(contaLabel).addComponent(conta));
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(valorLabel).addComponent(valor));
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(deposita).addComponent(cancela));
		layout.setVerticalGroup(vGroup);
	}

	private void criaGruposHorizontais(GroupLayout layout) {
		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
		hGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(contaLabel).addComponent(valorLabel)
				.addComponent(deposita));
		hGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(conta).addComponent(valor).addComponent(cancela));
		layout.setHorizontalGroup(hGroup);
	}

	private void organizaFields(GroupLayout layout) {
		criaGruposHorizontais(layout);
		criaGruposVerticais(layout);
	}

	class acao implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (arg0.getSource() == deposita) {
				if (ContaControlador.validaDadosConta(valor.getText(), conta
						.getText())) {
					if (ContaControlador.contaExists(Integer.parseInt(conta
							.getText()))) {

						String[] valores = valor.getText().split(",");
						int reais = Integer.parseInt(valores[0]);
						int centavos;

						try {
							centavos = Integer.parseInt(valores[1]);
						} catch (ArrayIndexOutOfBoundsException e) {
							centavos = 0;
						} catch (NumberFormatException e) {
							centavos = 0;
						}

						ContaControlador.depositar(reais, centavos, Integer
								.parseInt(conta.getText()));
						tela.dispose();
						try {
							new ClienteView().iniciaTela();
						} catch (NumberFormatException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					} else
						JOptionPane.showMessageDialog(tela,
								"A conta não existe");
				} else
					JOptionPane.showMessageDialog(tela, "Dados Incorretos");
			}
			if (arg0.getSource() == cancela) {
				tela.dispose();
				try {
					new ClienteView().iniciaTela();
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
